package com.mlethe.library.recyclerview.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.mlethe.library.recyclerview.listener.ItemDecorationListener;

/**
 * 水平分割线
 *
 * @author Mlethe
 * @date 2018/1/11
 */
public class LinearItemDecoration extends RecyclerView.ItemDecoration {

    private Context mContext;
    private Drawable mDivider;
    private Drawable mMarginDivider;
    /**
     * 分割线大小
     */
    private int mSize;
    /**
     * 顶部Margin大小
     */
    private int mTopMarginSize;
    /**
     * 底部Margin大小
     */
    private int mBottomMarginSize;
    /**
     * 左边Margin大小
     */
    private int mLeftMarginSize;
    /**
     * 右边Margin大小
     */
    private int mRightMarginSize;

    /**
     * 线条 Margin
     */
    private int mLineMarginSize;

    private boolean mReverseLayout;

    private int mOrientation = -1;

    private ItemDecorationListener mItemDecorationListener;

    public LinearItemDecoration() {
    }

    public LinearItemDecoration(Context context) {
        mContext = context.getApplicationContext();
    }

    public LinearItemDecoration setContext(Context context) {
        mContext = context.getApplicationContext();
        return this;
    }

    /**
     * 设置drawable
     *
     * @param drawableId
     */
    public LinearItemDecoration setDrawable(@DrawableRes int drawableId) {
        if (mContext == null) {
            return this;
        }
        return setDrawable(ContextCompat.getDrawable(mContext, drawableId));
    }

    /**
     * 设置drawable
     *
     * @param drawable
     */
    public LinearItemDecoration setDrawable(Drawable drawable) {
        mDivider = drawable;
        return this;
    }

    /**
     * 设置颜色
     *
     * @param colorRes
     */
    public LinearItemDecoration setColor(@ColorRes int colorRes) {
        if (mContext == null) {
            return this;
        }
        return setColorInt(ContextCompat.getColor(mContext, colorRes));
    }

    /**
     * 设置颜色
     *
     * @param color
     */
    public LinearItemDecoration setColorInt(@ColorInt int color) {
        mDivider = new ColorDrawable(color);
        return this;
    }

    /**
     * 设置颜色
     *
     * @param colorString
     */
    public LinearItemDecoration setColor(String colorString) {
        return setColorInt(Color.parseColor(colorString));
    }

    /**
     * 设置边框drawable
     *
     * @param drawableId
     */
    public LinearItemDecoration setMarginDrawable(@DrawableRes int drawableId) {
        if (mContext == null) {
            return this;
        }
        return setMarginDrawable(ContextCompat.getDrawable(mContext, drawableId));
    }

    /**
     * 设置边框drawable
     *
     * @param drawable
     */
    public LinearItemDecoration setMarginDrawable(Drawable drawable) {
        mMarginDivider = drawable;
        return this;
    }

    /**
     * 设置边框颜色
     *
     * @param colorRes
     */
    public LinearItemDecoration setMarginColor(@ColorRes int colorRes) {
        if (mContext == null) {
            return this;
        }
        return setMarginColorInt(ContextCompat.getColor(mContext, colorRes));
    }

    /**
     * 设置边框颜色
     *
     * @param color
     */
    public LinearItemDecoration setMarginColorInt(@ColorInt int color) {
        mMarginDivider = new ColorDrawable(color);
        return this;
    }

    /**
     * 设置边框颜色
     *
     * @param colorString
     */
    public LinearItemDecoration setMarginColor(String colorString) {
        return setMarginColorInt(Color.parseColor(colorString));
    }

    /**
     * 设置分割线大小
     *
     * @param size dp
     * @return
     */
    public LinearItemDecoration setSize(float size) {
        return setSize(dp2px(size));
    }

    /**
     * 设置分割线大小
     *
     * @param size px
     * @return
     */
    public LinearItemDecoration setSize(int size) {
        this.mSize = size;
        return this;
    }

    /**
     * 设置Margin大小（dp）
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @return
     */
    public LinearItemDecoration setMarginSize(float left, float top, float right, float bottom) {
        return setMarginSize(dp2px(left), dp2px(top), dp2px(right), dp2px(bottom));
    }

    /**
     * 设置Margin大小（px）
     *
     * @param left   左
     * @param top    上
     * @param right  右
     * @param bottom 下
     * @return
     */
    public LinearItemDecoration setMarginSize(int left, int top, int right, int bottom) {
        this.mLeftMarginSize = left;
        this.mTopMarginSize = top;
        this.mRightMarginSize = right;
        this.mBottomMarginSize = bottom;
        return this;
    }

    /**
     * 设置Margin大小（dp）
     *
     * @param size
     * @return
     */
    public LinearItemDecoration setMarginSize(float size) {
        return setMarginSize(dp2px(size));
    }

    /**
     * 设置Margin大小（px）
     *
     * @param size
     * @return
     */
    public LinearItemDecoration setMarginSize(int size) {
        this.mLeftMarginSize = size;
        this.mTopMarginSize = size;
        this.mRightMarginSize = size;
        this.mBottomMarginSize = size;
        return this;
    }

    /**
     * 设置左右Margin大小
     *
     * @param size dp
     * @return
     */
    public LinearItemDecoration setHorizontalMarginSize(float size) {
        return setHorizontalMarginSize(dp2px(size));
    }

    /**
     * 设置左右Margin大小
     *
     * @param size px
     * @return
     */
    public LinearItemDecoration setHorizontalMarginSize(int size) {
        this.mLeftMarginSize = size;
        this.mRightMarginSize = size;
        return this;
    }

    /**
     * 设置上下Margin大小
     *
     * @param size dp
     * @return
     */
    public LinearItemDecoration setVerticalMarginSize(float size) {
        return setVerticalMarginSize(dp2px(size));
    }

    /**
     * 设置上下Margin大小
     *
     * @param size px
     * @return
     */
    public LinearItemDecoration setVerticalMarginSize(int size) {
        this.mTopMarginSize = size;
        this.mBottomMarginSize = size;
        return this;
    }

    /**
     * 设置左右Margin大小
     *
     * @param left  dp
     * @param right dp
     * @return
     */
    public LinearItemDecoration setHorizontalMarginSize(float left, float right) {
        return setHorizontalMarginSize(dp2px(left), dp2px(right));
    }

    /**
     * 设置左右Margin大小
     *
     * @param left  px
     * @param right px
     * @return
     */
    public LinearItemDecoration setHorizontalMarginSize(int left, int right) {
        this.mLeftMarginSize = left;
        this.mRightMarginSize = right;
        return this;
    }

    /**
     * 设置上下Margin大小
     *
     * @param top    dp
     * @param bottom dp
     * @return
     */
    public LinearItemDecoration setVerticalMarginSize(float top, float bottom) {
        return setVerticalMarginSize(dp2px(top), dp2px(bottom));
    }

    /**
     * 设置上下Margin大小
     *
     * @param top    px
     * @param bottom px
     * @return
     */
    public LinearItemDecoration setVerticalMarginSize(int top, int bottom) {
        this.mTopMarginSize = top;
        this.mBottomMarginSize = bottom;
        return this;
    }

    /**
     * 线条 Margin
     *
     * @param marginSize dp
     * @return
     */
    public LinearItemDecoration setLineMarginSize(float marginSize) {
        return setLineMarginSize(dp2px(marginSize));
    }

    /**
     * 线条 Margin
     *
     * @param marginSize px
     * @return
     */
    public LinearItemDecoration setLineMarginSize(int marginSize) {
        this.mLineMarginSize = marginSize;
        return this;
    }

    /**
     * 设置自定义分割线
     *
     * @param itemDecorationListener
     * @return
     */
    public LinearItemDecoration setItemDecorationListener(ItemDecorationListener itemDecorationListener) {
        this.mItemDecorationListener = itemDecorationListener;
        return this;
    }

    /**
     * dp转px
     *
     * @param dp
     * @return
     */
    private int dp2px(float dp) {
        if (mContext == null) {
            return 0;
        }
        return dp2px(mContext, dp);
    }

    /**
     * dp转px
     *
     * @param dp
     * @return
     */
    protected int dp2px(Context context, float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }

    /**
     * 设置条目周边的偏移量
     *
     * @param outRect 接收输出的Rect
     * @param view    childView
     * @param parent  RecyclerView
     * @param state   RecyclerView的当前状态
     */
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        mContext = null;
        // 代表在每个底部的位置留出10px来绘制分割线 最后一个位置不需要分割线
        int position = parent.getChildAdapterPosition(view);
        if (position < 0) {
            return;
        }
        RecyclerView.Adapter adapter = parent.getAdapter();
        if (adapter == null) {
            return;
        }
        int itemCount = adapter.getItemCount();
        if (position >= itemCount) {
            return;
        }
        if (mItemDecorationListener != null) {
            int startPosition = mItemDecorationListener.getStart();
            int dataSize = mItemDecorationListener.getDrawCount();
            if (startPosition >= 0 && position >= startPosition && dataSize > 0) {
                position -= startPosition;
                itemCount = dataSize;
                if (position >= itemCount) {
                    return;
                }
            } else {
                return;
            }
        }
        // parent.getChildCount() 是不断变化的 现在没办法保证最后一条
        // 保证第一条
        int left = 0;
        int top = 0;
        int right = 0;
        int bottom = 0;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager == null || layoutManager instanceof GridLayoutManager || layoutManager instanceof StaggeredGridLayoutManager) {
            return;
        }
        LinearLayoutManager manager = (LinearLayoutManager) layoutManager;
        mReverseLayout = manager.getReverseLayout();
        mOrientation = manager.getOrientation();
        if (mReverseLayout) {
            if (mOrientation == LinearLayoutManager.HORIZONTAL) {
                if (position == 0) {
                    right = mRightMarginSize;
                } else {
                    right = mSize;
                }
                if (position == itemCount - 1) {
                    left = mLeftMarginSize;
                }
                top = mTopMarginSize;
                bottom = mBottomMarginSize;
            } else if (mOrientation == LinearLayoutManager.VERTICAL) {
                if (position == 0) {
                    bottom = mBottomMarginSize;
                } else {
                    bottom = mSize;
                }
                if (position == itemCount - 1) {
                    top = mTopMarginSize;
                }
                left = mLeftMarginSize;
                right = mRightMarginSize;
            }
            outRect.set(left, top, right, bottom);
            return;
        }
        if (mOrientation == LinearLayoutManager.HORIZONTAL) {
            if (position == 0) {
                left = mLeftMarginSize;
            } else {
                left = mSize;
            }
            if (position == itemCount - 1) {
                right = mRightMarginSize;
            }
            top = mTopMarginSize;
            bottom = mBottomMarginSize;
        } else if (mOrientation == LinearLayoutManager.VERTICAL) {
            if (position == 0) {
                top = mTopMarginSize;
            } else {
                top = mSize;
            }
            if (position == itemCount - 1) {
                bottom = mBottomMarginSize;
            }
            left = mLeftMarginSize;
            right = mRightMarginSize;
        }
        outRect.set(left, top, right, bottom);
    }

    /**
     * 绘制分割线
     *
     * @param canvas
     * @param parent
     * @param state
     */
    @Override
    public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        if (mReverseLayout) {
            if (mOrientation == LinearLayoutManager.HORIZONTAL) {
                reverseDrawHorizontalDividers(canvas, parent, state);
            } else if (mOrientation == LinearLayoutManager.VERTICAL) {
                reverseDrawDividers(canvas, parent, state);
            }
            return;
        }
        if (mOrientation == LinearLayoutManager.HORIZONTAL) {
            drawHorizontalDividers(canvas, parent, state);
        } else if (mOrientation == LinearLayoutManager.VERTICAL) {
            drawDividers(canvas, parent, state);
        }
    }

    /**
     * 绘制在图层的最上层
     *
     * @param canvas 画布
     * @param parent RecyclerView
     * @param state  RecyclerView的当前状态
     */
    @Override
    public void onDrawOver(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    }

    /**
     * 正向绘制（水平方向滑动）
     *
     * @param canvas 将在其上绘制水平分隔符的画布
     * @param parent RecyclerView
     * @param state  RecyclerView的当前状态
     */
    private void drawHorizontalDividers(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int childCount = parent.getChildCount();
        RecyclerView.Adapter adapter = parent.getAdapter();
        if (adapter == null) {
            return;
        }
        int itemCount = adapter.getItemCount();
        int startPosition = -1;
        if (mItemDecorationListener != null) {
            startPosition = mItemDecorationListener.getStart();
            int dataSize = mItemDecorationListener.getDrawCount();
            if (startPosition >= 0 && dataSize > 0) {
                itemCount = dataSize;
            } else {
                return;
            }
        }

        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(childView);

            if (mItemDecorationListener != null && startPosition >= 0) {
                if (position >= startPosition) {
                    position -= startPosition;
                } else {
                    continue;
                }
            }

            if (position < 0 || position >= itemCount) {
                continue;
            }

            // 计算水平分割线的位置
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) childView.getLayoutParams();
            // 画水平分隔线
            if (mMarginDivider != null && mTopMarginSize > 0) {
                int left = childView.getLeft() - layoutParams.leftMargin;
                if (position == 0) {
                    left -= mLeftMarginSize;
                } else {
                    left -= mSize;
                }
                int right = childView.getRight() + layoutParams.rightMargin;
                if (position == itemCount - 1) {
                    right += mRightMarginSize;
                }
                int bottom = childView.getTop() - layoutParams.topMargin;
                int top = bottom - mTopMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            if (mMarginDivider != null && mBottomMarginSize > 0) {
                int left = childView.getLeft() - layoutParams.leftMargin;
                if (position == 0) {
                    left -= mLeftMarginSize;
                } else {
                    left -= mSize;
                }
                int right = childView.getRight() + layoutParams.rightMargin;
                if (position == itemCount - 1) {
                    right += mRightMarginSize;
                }
                int top = childView.getBottom() + layoutParams.bottomMargin;
                int bottom = top + mBottomMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }

            // 画垂直分割线
            if (position == itemCount - 1 && mMarginDivider != null && mRightMarginSize > 0) {
                int left = childView.getRight() + layoutParams.rightMargin;
                int right = left + mRightMarginSize;
                int top = childView.getTop() - layoutParams.topMargin;
                int bottom = childView.getBottom() + layoutParams.bottomMargin;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            if (position == 0) {
                if (mMarginDivider != null && mLeftMarginSize > 0) {
                    int right = childView.getLeft() - layoutParams.leftMargin;
                    int left = right - mLeftMarginSize;
                    int top = childView.getTop() - layoutParams.topMargin;
                    int bottom = childView.getBottom() + layoutParams.bottomMargin;
                    mMarginDivider.setBounds(left, top, right, bottom);
                    mMarginDivider.draw(canvas);
                }
            } else {
                if (mDivider != null && mSize > 0) {
                    int top = childView.getTop() - layoutParams.topMargin + mLineMarginSize;
                    int bottom = childView.getBottom() + layoutParams.bottomMargin - mLineMarginSize;
                    int right = childView.getLeft() - layoutParams.leftMargin;
                    int left = right - mSize;
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(canvas);
                }
            }
        }
    }

    /**
     * 正向绘制（垂直方向滑动）
     *
     * @param canvas 画布
     * @param parent RecyclerView
     * @param state  RecyclerView的当前状态
     */
    private void drawDividers(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int childCount = parent.getChildCount();
        RecyclerView.Adapter adapter = parent.getAdapter();
        if (adapter == null) {
            return;
        }
        int itemCount = adapter.getItemCount();
        int startPosition = -1;
        if (mItemDecorationListener != null) {
            startPosition = mItemDecorationListener.getStart();
            int dataSize = mItemDecorationListener.getDrawCount();
            if (startPosition >= 0 && dataSize > 0) {
                itemCount = dataSize;
            } else {
                return;
            }
        }

        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(childView);

            if (mItemDecorationListener != null && startPosition >= 0) {
                if (position >= startPosition) {
                    position -= startPosition;
                } else {
                    continue;
                }
            }

            if (position < 0 || position >= itemCount) {
                continue;
            }

            // 计算水平分割线的位置
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) childView.getLayoutParams();
            // 画垂直分割线
            if (mMarginDivider != null && mLeftMarginSize > 0) {
                int top = childView.getTop() - layoutParams.topMargin;
                int bottom = childView.getBottom() + layoutParams.bottomMargin;
                if (position != itemCount - 1) {
                    bottom += mSize;
                }
                int right = childView.getLeft() - layoutParams.leftMargin;
                int left = right - mLeftMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            if (mMarginDivider != null && mRightMarginSize > 0) {
                int top = childView.getTop() - layoutParams.topMargin;
                int bottom = childView.getBottom() + layoutParams.bottomMargin;
                if (position != itemCount - 1) {
                    bottom += mSize;
                }
                int left = childView.getRight() + layoutParams.rightMargin;
                int right = left + mRightMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            // 画水平分隔线
            if (mMarginDivider != null && mBottomMarginSize > 0 && position == itemCount - 1) {
                int top = childView.getBottom() + layoutParams.bottomMargin;
                int bottom = top + mBottomMarginSize;
                int left = childView.getLeft() - layoutParams.leftMargin - mLeftMarginSize;
                int right = childView.getRight() + layoutParams.rightMargin + mRightMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            if (position == 0) {
                if (mMarginDivider != null && mTopMarginSize > 0) {
                    int parentBottom = childView.getTop() - layoutParams.topMargin;
                    int parentTop = parentBottom - mTopMarginSize;
                    int left = childView.getLeft() - layoutParams.leftMargin - mLeftMarginSize;
                    int right = childView.getRight() + layoutParams.rightMargin + mRightMarginSize;
                    mMarginDivider.setBounds(left, parentTop, right, parentBottom);
                    mMarginDivider.draw(canvas);
                }
            } else {
                if (mDivider != null && mSize > 0) {
                    int left = childView.getLeft() - layoutParams.leftMargin + mLineMarginSize;
                    int right = childView.getRight() + layoutParams.rightMargin - mLineMarginSize;
                    int bottom = childView.getTop() - layoutParams.topMargin;
                    int top = bottom - mSize;
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(canvas);
                }
            }
        }
    }

    /**
     * 反向绘制（水平方向滑动）
     *
     * @param canvas 将在其上绘制水平分隔符的画布
     * @param parent RecyclerView
     * @param state  RecyclerView的当前状态
     */
    private void reverseDrawHorizontalDividers(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int childCount = parent.getChildCount();
        RecyclerView.Adapter adapter = parent.getAdapter();
        if (adapter == null) {
            return;
        }
        int itemCount = adapter.getItemCount();
        int startPosition = -1;
        if (mItemDecorationListener != null) {
            startPosition = mItemDecorationListener.getStart();
            int dataSize = mItemDecorationListener.getDrawCount();
            if (startPosition >= 0 && dataSize > 0) {
                itemCount = dataSize;
            } else {
                return;
            }
        }

        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(childView);

            if (mItemDecorationListener != null && startPosition >= 0) {
                if (position >= startPosition) {
                    position -= startPosition;
                } else {
                    continue;
                }
            }

            if (position < 0 || position >= itemCount) {
                continue;
            }

            // 计算水平分割线的位置
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) childView.getLayoutParams();
            // 画水平分隔线
            if (mMarginDivider != null && mTopMarginSize > 0) {
                int left = childView.getLeft() - layoutParams.leftMargin;
                if (position == itemCount - 1) {
                    left -= mLeftMarginSize;
                } else {
                    left -= mSize;
                }
                int right = childView.getRight() + layoutParams.rightMargin;
                if (position == 0) {
                    right += mRightMarginSize;
                }
                int bottom = childView.getTop() - layoutParams.topMargin;
                int top = bottom - mTopMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            if (mMarginDivider != null && mBottomMarginSize > 0) {
                int left = childView.getLeft() - layoutParams.leftMargin;
                if (position == itemCount - 1) {
                    left -= mLeftMarginSize;
                } else {
                    left -= mSize;
                }
                int right = childView.getRight() + layoutParams.rightMargin;
                if (position == 0) {
                    right += mRightMarginSize;
                }
                int top = childView.getBottom() + layoutParams.bottomMargin;
                int bottom = top + mBottomMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }

            // 画垂直分割线
            if (position == 0 && mMarginDivider != null && mRightMarginSize > 0) {
                int left = childView.getRight() + layoutParams.rightMargin;
                int right = left + mRightMarginSize;
                int top = childView.getTop() - layoutParams.topMargin;
                int bottom = childView.getBottom() + layoutParams.bottomMargin;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            if (position == itemCount - 1) {
                if (mMarginDivider != null && mLeftMarginSize > 0) {
                    int right = childView.getLeft() - layoutParams.leftMargin;
                    int left = right - mLeftMarginSize;
                    int top = childView.getTop() - layoutParams.topMargin;
                    int bottom = childView.getBottom() + layoutParams.bottomMargin;
                    mMarginDivider.setBounds(left, top, right, bottom);
                    mMarginDivider.draw(canvas);
                }
            } else {
                if (mDivider != null && mSize > 0) {
                    int top = childView.getTop() - layoutParams.topMargin + mLineMarginSize;
                    int bottom = childView.getBottom() + layoutParams.bottomMargin - mLineMarginSize;
                    int right = childView.getLeft() - layoutParams.leftMargin;
                    int left = right - mSize;
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(canvas);
                }
            }
        }
    }

    /**
     * 反向绘制（垂直方向滑动）
     *
     * @param canvas 画布
     * @param parent RecyclerView
     * @param state  RecyclerView的当前状态
     */
    private void reverseDrawDividers(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int childCount = parent.getChildCount();
        RecyclerView.Adapter adapter = parent.getAdapter();
        if (adapter == null) {
            return;
        }
        int itemCount = adapter.getItemCount();
        int startPosition = -1;
        if (mItemDecorationListener != null) {
            startPosition = mItemDecorationListener.getStart();
            int dataSize = mItemDecorationListener.getDrawCount();
            if (startPosition >= 0 && dataSize > 0) {
                itemCount = dataSize;
            } else {
                return;
            }
        }

        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(childView);

            if (mItemDecorationListener != null && startPosition >= 0) {
                if (position >= startPosition) {
                    position -= startPosition;
                } else {
                    continue;
                }
            }

            if (position < 0 || position >= itemCount) {
                continue;
            }

            // 计算水平分割线的位置
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) childView.getLayoutParams();
            // 画垂直分割线
            if (mMarginDivider != null && mLeftMarginSize > 0) {
                int top = childView.getTop() - layoutParams.topMargin;
                if (position != itemCount - 1) {
                    top -= mSize;
                }
                int bottom = childView.getBottom() + layoutParams.bottomMargin;
                int right = childView.getLeft() - layoutParams.leftMargin;
                int left = right - mLeftMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            if (mMarginDivider != null && mRightMarginSize > 0) {
                int top = childView.getTop() - layoutParams.topMargin;
                if (position != itemCount - 1) {
                    top -= mSize;
                }
                int bottom = childView.getBottom() + layoutParams.bottomMargin;
                int left = childView.getRight() + layoutParams.rightMargin;
                int right = left + mRightMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            // 画水平分隔线
            if (mMarginDivider != null && mBottomMarginSize > 0 && position == 0) {
                int top = childView.getBottom() + layoutParams.bottomMargin;
                int bottom = top + mBottomMarginSize;
                int left = childView.getLeft() - layoutParams.leftMargin - mLeftMarginSize;
                int right = childView.getRight() + layoutParams.rightMargin + mRightMarginSize;
                mMarginDivider.setBounds(left, top, right, bottom);
                mMarginDivider.draw(canvas);
            }
            if (position == itemCount - 1) {
                if (mMarginDivider != null && mTopMarginSize > 0) {
                    int parentBottom = childView.getTop() - layoutParams.topMargin;
                    int parentTop = parentBottom - mTopMarginSize;
                    int left = childView.getLeft() - layoutParams.leftMargin - mLeftMarginSize;
                    int right = childView.getRight() + layoutParams.rightMargin + mRightMarginSize;
                    mMarginDivider.setBounds(left, parentTop, right, parentBottom);
                    mMarginDivider.draw(canvas);
                }
            } else {
                if (mDivider != null && mSize > 0) {
                    int left = childView.getLeft() - layoutParams.leftMargin + mLineMarginSize;
                    int right = childView.getRight() + layoutParams.rightMargin - mLineMarginSize;
                    int bottom = childView.getTop() - layoutParams.topMargin;
                    int top = bottom - mSize;
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(canvas);
                }
            }
        }
    }
}